//
//  CameraBottomView.m
//  Version1.0_RectiveCocoa
//
//  Created by 吴可高 on 2018/1/22.
//  Copyright © 2018年 吴可高. All rights reserved.
//

#import "CameraBottomView.h"

@interface CameraBottomView()
@property(nonatomic,strong) UIButton * camera_item;
@property(nonatomic,strong) NSMutableArray * items;
@end
@implementation CameraBottomView

#pragma mark - IBOutlet ActionEvents
-(void)photoFromSessionStartRunning:(UIButton*)args{
    if(self.handler){
        self.handler(args);
    }
}
-(void)itemDidSelected:(UIButton*)args{
    if(self.handler){
        self.handler(args);
    }
}

-(UIButton *)camera_item{
    _camera_item = ({
        if (!_camera_item) {
            _camera_item = [UIButton buttonWithType:UIButtonTypeCustom];
            _camera_item.layer.masksToBounds = YES;
            _camera_item.layer.borderWidth = 6.f;
            _camera_item.layer.borderColor = [UIColor whiteColor].CGColor;
            _camera_item.backgroundColor = UIColorFromRGB(0xFF4650);
            _camera_item.layer.cornerRadius = 65/2.f;
            _camera_item.tag = 1000;
            [_camera_item addTarget:self action:@selector(photoFromSessionStartRunning:) forControlEvents:UIControlEventTouchUpInside];
        }
        _camera_item;
    });
    return _camera_item;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)setUp{
    __weak  typeof(self)weakSelf = self;
     self.backgroundColor = UIColorFromRGB(0x151724);
    [self addSubview:self.camera_item];
    [self.camera_item mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(65, 65));
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.mas_centerY);
    }];
    [self loadSubViews];
}
-(void)loadSubViews{
    __weak  typeof(self)weakSelf = self;
    __block NSMutableArray * containers = [NSMutableArray array];
    for (int i =0; i < 2; i++) {
        UIButton * item = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage * img =  (i == 0) ? [UIImage imageNamed:@"lq_paishe_xiangce"] : [UIImage imageNamed:@"qmx_paishe_qiehuanjingtou"];
        item.tag = 1001 + i;
        [item setImage:img forState:UIControlStateNormal];
        [item addTarget:self action:@selector(itemDidSelected:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:item];
        [containers addObject:item];
    }
    if (containers == nil) return;
    __block UIButton * anchor = nil;
    [containers enumerateObjectsUsingBlock:^(UIButton* item, NSUInteger idx, BOOL * _Nonnull stop) {
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(45, 45));
            make.centerY.mas_equalTo(weakSelf.mas_centerY).with.offset(0.f);
            if (anchor == nil) {
                make.centerX.mas_equalTo(weakSelf.mas_centerX).multipliedBy(0.5).offset(-45/2.f);
            }else{
                make.centerX.mas_equalTo(weakSelf.mas_centerX).multipliedBy(1.5).offset(45/2.f);
            }
            anchor = item;
        }];
    }];
}
@end
